package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.User;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IUserService;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author chemin
 * @since 2024/7/5
 */
@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "管理员接口")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        try {
            List<User> list = iUserService.list();
            if(!CollectionUtils.isEmpty(list)){
                return new Result(true , "查询成功", list);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    @GetMapping("findPage")
    @ApiOperation("分页条件查询用户列表")
    public Result findPage(PageQueryVO vo){
        Page<User> page = iUserService.findPage(vo);
        if(page!=null){
            return new Result(true , "查询用户列表成功" , page);
        }
        return new Result(false , "查询用户列表失败");
    }

    @GetMapping("{id}")
    @ApiOperation("根据用户ID查询信息")
    public Result findOneById(@PathVariable("id")Integer userId){
        User user = iUserService.getById(userId);
        if(user!=null){
            return new Result(true , "查询用户成功" , user);
        }
        return new Result(false , "查询用户失败");
    }


    @PostMapping("upload")
    @ApiOperation("用户图片上传")
    public Result upload(MultipartFile imgFile) throws IOException {
        //返回的是图片名字，拼接上七牛云的域名进行显示
        String pic = iUserService.uploadByQiniuyun(imgFile);

        //返回的是外链地址，可用于直接显示
        //String pic = setmealService.uploadByAliyun(imgFile);
        if(StringUtils.hasLength(pic)){
            return new Result(true,"上传图片失败", (Object) pic);
        }
        return new Result(false , "上传图片失败");
    }


}
